package sunwul.hospital.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sunwul.hospital.hosp.mapper.DepartmentMapper;
import sunwul.hospital.hosp.service.DepartmentService;
import sunwul.hospital.hosp.util.CheckUtil;
import sunwul.hospital.model.model.hosp.Department;
import sunwul.hospital.model.vo.hosp.DepartmentQueryVo;
import sunwul.hospital.model.vo.hosp.DepartmentVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunwul
 * @date 2023/11/4 17:00:37
 * @description
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper,Department> implements DepartmentService {

    @Autowired
    CheckUtil checkUtil;

    /**
     * 分页查询科室信息
     * @param map
     */
    @Override
    public Map<String, Object> findDepartment(Map<String, Object> map) {
        // 校验
        checkUtil.checkSignKey(map);
        String hoscode = (String) map.get("hoscode");
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page")); // 当前页
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String) map.get("limit")); // 每页大小

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setDepcode(hoscode);

        Page<Department> pageMode = new Page<>(page, limit);
        QueryWrapper<Department> wrapper = new QueryWrapper<Department>().eq("hoscode", hoscode);
        Page<Department> departmentPage = baseMapper.selectPage(pageMode, wrapper);
        // 匹配接口返回结果
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("totalElements",departmentPage.getTotal());
        resMap.put("pageNum",departmentPage.getCurrent());
        resMap.put("content",departmentPage.getRecords());
        return resMap;
    }

    /**
     * 查询医院下的所有科室列表
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();
        List<Department> departments = baseMapper.selectList(new QueryWrapper<Department>().eq("hoscode", hoscode));
        // 根据上级编号bigcode进行分组
        Map<String, List<Department>> groupMap = departments.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : groupMap.entrySet()) {
            // 大科室编号bigcode
            String bigcode = entry.getKey();
            // 大科室编号对应的所有数据
            List<Department> deptList = entry.getValue();
            // 大科室信息
            DepartmentVo vo = new DepartmentVo();
            vo.setDepcode(bigcode);
            vo.setDepname(deptList.get(0).getBigname());
            // 小科室信息
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : deptList) {
                DepartmentVo vo1 = new DepartmentVo();
                vo1.setDepcode(department.getDepcode());
                vo1.setDepname(department.getDepname());
                children.add(vo1);
            }
            vo.setChildren(children);
            result.add(vo);
        }
        return result;
    }


}
